<template>
  <div class="user-management-container">
    <el-table
      v-loading="listLoading"
      border
      :data="list"
      @selection-change="setSelectRows"
    >
      <el-table-column align="center" show-overflow-tooltip type="selection" />

      <el-table-column
        align="center"
        label="id"
        prop="id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="地区名称"
        prop="areaName"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        label="操作"
        show-overflow-tooltip
        width="130"
      >
        <template #default="{ row }">
          <template
            v-if="row.areaName != '东方影都' && row.areaName != '其他地区'"
          >
            <el-button type="text" @click="handleEdit(row)">编辑</el-button>
            <el-button type="text" @click="handleDelete(row)">删除</el-button>
          </template>
        </template>
      </el-table-column>
      <template #empty>
        <el-image
          class="vab-data-empty"
          :src="require('@/assets/empty_images/data_empty.png')"
        />
      </template>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { getList, doDelete } from '@/api/diqu'
  import edit from './model/edit.vue'

  export default {
    name: 'UserManagement',
    components: { edit },
    data() {
      return {
        list: [],
        options2: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 10,
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      async chooseone() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      setSelectRows(val) {
        this.selectRows = val
      },
      handleEdit(row) {
        //编辑
        if (row.id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },

      baoming(row) {
        //报名管理
        if (row.id) {
          this.$refs['bmgl'].showEdit(row)
        } else {
          this.$refs['bmgl'].showEdit()
        }
      },
      lingdui(row) {
        console.log(row.jobId)
        //领队管理
        if (row.id) {
          this.$refs['ldgl'].showEdit(row)
        } else {
          this.$refs['ldgl'].showEdit()
        }
      },
      handleEditfour(row) {
        //停用
        if (row.id) {
          this.$refs['Editxiugai'].showEdit(row)
        } else {
          this.$refs['Editxiugai'].showEdit()
        }
      },
      handleDelete(row) {
        if (row.id) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ id: row.id })
            if (msg === 'success') {
              this.$baseMessage(
                '删除成功',
                'success',
                'vab-hey-message-success'
              )
              await this.fetchData()
            }
          })
        } else {
          if (this.selectRows.length > 0) {
            const id = this.selectRows.map((item) => item.id).join()
            this.$baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ id })
              console.log(msg)
              this.$baseMessage(msg.data, 'success', 'vab-hey-message-success')
              await this.fetchData()
            })
          } else {
            this.$baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },

      async fetchData() {
        this.listLoading = true
        const res = await getList(this.queryForm)

        this.list = res.data.dataList
        this.total = res.data.total
        this.listLoading = false
      },
    },
  }
</script>
